﻿@using ST = EmoneyWeiXinCommon.StringTools
@{
    ViewBag.Title = "WeixinUser";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="right">
    <h3>微信公众号配置</h3>
    <!--manage-->
    <div class="manage">
        <input type="hidden" id="hidWeixinUserid" value="0" />
        <!--manage_top-->
        <div class="manage_top" style="display: none;">
            <div class="manage_register">
                <p><span>微信公众号名称：</span><input type="text" id="txtTitle"></p>
                <p><span>微信运营团队：</span><input type="text" id="txtAuthor"></p>
            </div>
            <div class="manage_register">
                <p><span>AppID(应用ID)：</span><input type="text" id="txtAppId"></p>
                <p><span>AppSecret(应用密钥)：</span><input type="text" id="txtAppSecret"></p>
            </div>
            <div class="manage_register">
                <p><span>微信原始ID：</span><input type="text" id="txtToken"></p>
                <p><span>消息加解密密钥：</span><input type="text" id="txtEncodingAESKey"></p>
            </div>
            <div class="manage_register">
                <p><span>微信号：</span><input type="text" id="txtWeiXinNumber"></p>
                <p><span>微信号描述(生成二维码用)：</span><input type="text" id="txtRemark"></p>
            </div>
            <div class="manage_register">
                <p class="manage_register_p"><span>URL(服务器地址)：</span><input type="text" id="txtServicesUrl"></p>
            </div>
            <div class="manage_register">
                <p class="manage_register_p"><span>MP平台关注页URL：：</span><input type="text" id="txtgzUrl"></p>
            </div>
            <div class="manage_register">
                <div class="manage_register_p02">
                    <span>是否关联微社区：</span>
                    <input type="radio" name="bntMrec" id="IsMicrocommunity1"><label>是</label><input type="radio" name="bntMrec" checked="checked" id="IsMicrocommunity2"><label>否</label>
                    <span class="manage_register_font03" style="display: none;">微社区地址：</span>
                    <input type="text" id="txtMicrocommunityUrl" style="display: none;">
                </div>
            </div>
            <p class="manage_add">
                <a href="javascript:void(0);" id="divAdd">确认</a>
                <a href="javascript:void(0);" class="manage_add_font">取消</a>
            </p>
            <!--manage_top end-->
            <p class="manage_line"></p>
        </div>

        <p id="divmanager" style="background: #0AAF48; border: 1px solid #D0D0D0; height: 26px; line-height: 26px; width: 100px; padding: 0px 13px;">
            <a href="javascript:void(0);" style="color: #fff;">添加微信公众号</a>
        </p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <div id="WeiXinUser_Data">
            </div>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<script type="text/javascript">
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    $(window).ready(function () {
        getWeixinUser();
        
    });

    function PageCountHtml() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/GetPageHtmlCount")",
            async: true,
            data: { tableName: "weixinUser" },
            dataType: "html",
            success: function (result) {
                pageCount = Math.ceil(result / pageSize);
                if (pageCount == 0) {
                    pageCount = 1;
                }
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                $("#manage_page .manage_page_right").click(function () {
                    pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
                    $("#manage_page").html(PageHtml(pageIndex, pageCount));
                    getWeixinUser();
                });
                $("#manage_page .tiaozhuan").click(function () {
                    var pageNum = $("#txtPageNum").val();
                    try {
                        pageIndex = parseInt(pageNum);
                    } catch (e) {
                        alert("请输入数字");
                        return false;
                    }

                    if (pageNum > 0 && pageNum <= pageCount) {
                        $("#manage_page").html(PageHtml(pageIndex, pageCount));
                        getWeixinUser();
                    }
                    else {
                        alert("当前数字超出范围");
                        return false;
                    }
                })

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("error");
            }
        });
    }
    $("#IsMicrocommunity1").click(function () {
        $("#txtMicrocommunityUrl").show();
        $(".manage_register_font03").show();
    })
    $("#IsMicrocommunity2").click(function () {
        $("#txtMicrocommunityUrl").hide();
        $(".manage_register_font03").hide();
    })

    $("#divmanager").click(function () {

        $("#hidWeixinUserid").val("");
        $("#txtTitle").val("");
        $("#txtAuthor").val("");
        $("#txtAppId").val("");
        $("#txtAppSecret").val("");
        $("#txtToken").val("");
        $("#txtEncodingAESKey").val("");
        $("#txtServicesUrl").val("");
        $("#txtgzUrl").val("");
        $("#txtMicrocommunityUrl").val("");

        $("#txtWeiXinNumber").val("");
        $("#txtRemark").val("");
        $("#IsMicrocommunity2").click();

        $("#hidWeixinUserid").val("0");
        $(".manage_top").show();
        
    })
    $(".manage_top .manage_add_font").click(function () {
        $(".manage_top").hide();
    });

    $("#divAdd").click(function () {
        var title = $("#txtTitle").val();
        var Author = $("#txtAuthor").val();
        var AppId = $("#txtAppId").val();
        var AppSecret = $("#txtAppSecret").val();

        var Token = $("#txtToken").val();
        var EncodingAESKey = $("#txtEncodingAESKey").val();
        var ServicesUrl = $("#txtServicesUrl").val();
        var gzUrl = $("#txtgzUrl").val();
        var WeiXinNumber = $("#txtWeiXinNumber").val();
        var Remark = $("#txtRemark").val();
        var IsMicrocommunity = 0;
        var MicrocommunityUrl = "";
        if ($("#IsMicrocommunity1").attr("checked") != undefined) {
            IsMicrocommunity = 1;
            MicrocommunityUrl = $("#txtMicrocommunityUrl").val();
        }
        if (title == "") {
            alert("微信公众号名称不能为空");
            return false;
        }
        if (WeiXinNumber == "") {
            alert("微信号不能为空");
            return false;
        }
       
        var hidWeixinUserid = $("#hidWeixinUserid").val();
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/UpdateWeixinUser")",
            async: true,
            data: {
                title: encodeURIComponent(title),
                Author: encodeURIComponent(Author),
                AppId: encodeURIComponent(AppId),
                AppSecret: encodeURIComponent(AppSecret),
                hidWeixinUserid: encodeURIComponent(hidWeixinUserid),
                Token: encodeURIComponent(Token),
                EncodingAESKey: encodeURIComponent(EncodingAESKey),
                ServicesUrl: encodeURIComponent(ServicesUrl),
                gzUrl: encodeURIComponent(gzUrl),
                IsMicrocommunity: encodeURIComponent(IsMicrocommunity),
                MicrocommunityUrl: encodeURIComponent(MicrocommunityUrl),
                WeiXinNumber: encodeURIComponent(WeiXinNumber),
                Remark: encodeURIComponent(Remark)
            },
            dataType: "html",
            success: function (result) {
                if (result == "-99") {
                    alert("登录失效，请重新登录");
                }
                else if (result == "1") {
                    if (parseInt(hidWeixinUserid) > 0) {
                        alert("修改成功");
                    }
                    else {
                        alert("添加成功");
                    }
                    location.href = "@ST.GetTrendsFile("BaseConfiguration/WeixinUser")";
                }
                else {
                    alert("保存失败[" + result + "]");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("error");
            }
        });

    });



function getWeixinUser() {
    $.ajax({
        type: "get",
        url: "@ST.GetTrendsFile("BaseConfiguration/WeiXinUser_Data")" + "?random=" + Math.random(),
        async: true,
        data: { pageIndex: pageIndex, pagecount: pageSize },
        dataType: "html",
        success: function (result) {
            $("#WeiXinUser_Data").html(result);
            PageCountHtml();
            $("#WeiXinUser_Data .bianji").click(function () {
                var id = $(this).attr("typeid");
                $.ajax({
                    type: "get",
                    url: "@ST.GetTrendsFile("BaseConfiguration/getWeixinOne")" + "?random=" + Math.random(),
                    async: true,
                    data: { id: id },
                    dataType: "html",
                    success: function (result) {
                        if (result != "") {
                            $("#hidWeixinUserid").val("0");
                            $(".manage_top").show();
                            eval("var dataArr=" + result + ";");
                            $("#hidWeixinUserid").val(dataArr["id"]);
                            $("#txtTitle").val(dataArr["title"]);
                            $("#txtAuthor").val(dataArr["Author"]);
                            $("#txtAppId").val(dataArr["AppId"]);
                            $("#txtAppSecret").val(dataArr["AppSecret"]);

                            $("#txtToken").val(dataArr["Token"]);
                            $("#txtEncodingAESKey").val(dataArr["EncodingAESKey"]);
                            $("#txtServicesUrl").val(dataArr["servicesUrl"]);
                            $("#txtgzUrl").val(dataArr["gzUrl"]);
                            $("#txtMicrocommunityUrl").val(dataArr["MicrocommunityUrl"]);
                            $("#txtWeiXinNumber").val(dataArr["weixinNumber"]);
                            $("#txtRemark").val(dataArr["remark"]);
                            if (dataArr["IsMicrocommunity"] == 1) {
                                $("#IsMicrocommunity1").click();
                            }
                            else {
                                $("#IsMicrocommunity2").click();
                            }
                        }
                    }
                });
            });

            $("#WeiXinUser_Data .shanchu").click(function () {
                var id = $(this).attr("typeid");
                $.ajax({
                    type: "get",
                    url: "@ST.GetTrendsFile("BaseConfiguration/weixindel")",
                    async: true,
                    data: { id: id },
                    dataType: "html",
                    success: function (result) {
                        if (result > 0) {
                            alert("删除成功");
                            location.href = "@ST.GetTrendsFile("BaseConfiguration/WeixinUser")";
                        }
                    }
                });


            });
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("error");
        }
    });
}
</script>

